package com.bl.org.controller;

import com.bl.basic.annotation.PreAuthorize;
import com.bl.org.service.IDepartmentService;
import com.bl.org.domain.Department;
import com.bl.org.query.DepartmentQuery;
import com.bl.basic.util.JsonResult;
import com.bl.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/department")
//Swagger接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    public IDepartmentService departmentService;

    /**
     * 接口：添加或修改
     * @param department  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @ApiOperation(value = "添加和修改的接口" )
    @PreAuthorize(name = "部门添加和修改",sn = "department:save2update") //value后面用来做权限校验
    public JsonResult addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.save(department);
            System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwww"+department);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "部门删除",sn = "department:delete") //value后面用来做权限校验
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMessage("删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @ApiOperation(value = "批量删除接口" )
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDelete") //value后面用来做权限校验
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
                departmentService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询接口" )
    @PreAuthorize(name = "部门查询单个",sn = "department:findOne") //value后面用来做权限校验
    public Department findOne(@PathVariable("id")Long id) {
        return departmentService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @ApiOperation(value = "查询所有部门接口" )
    @PreAuthorize(name = "部门查询所有",sn = "department:findAll") //value后面用来做权限校验
    public List<Department> findAll(){
        return departmentService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询`
    * @param query 查询对象
    * @return PageList 分页对象
    */
   @PostMapping
   @ApiOperation(value = "分页查询部门接口" )
   @PreAuthorize(name = "部门分页查询",sn = "department:PageList") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query) {
       try {
           return   departmentService.queryPage(query);

       } catch (Exception e) {
           e.printStackTrace();
           return  new PageList<Department>();
       }

    }
    /**
     * 接口：获取上级部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询所有部门接口" )
    @PreAuthorize(name = "部门树",sn = "department:tree") //value后面用来做权限校验
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
